﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace PowerWaveAddin
{
    internal class PresentationManager
    {
        private Dictionary<string, PresentationInfo> dict;

        private PresentationInfo currentPresentation;

        public bool ContainsName(string name)
        {
            return dict.Keys.Contains(name);
        }

        public PresentationInfo Get(string name)
        {
            if (dict.Keys.Contains(name))
            {
                return dict[name];
            }

            return null;
        }

        public void SetCurrentPresentation(PresentationInfo pres)
        {
            this.dict[pres.Name] = pres;
            this.currentPresentation = pres;
        }

        public bool SetCurrentPresentationByName(string name)
        {
            if (this.dict.Keys.Contains(name))
            {
                this.currentPresentation = this.dict[name];
                return true;
            }
            return false;
        }

        public void ChangeCurrentPresentationKey(string newKey)
        {
            currentPresentation.Key = newKey;
            SetCurrentPresentation(currentPresentation);
        }

        public PresentationInfo GetCurrentPresentation()
        {
            return this.currentPresentation;
        }

        public ICollection<string> GetNameList()
        {
            return this.dict.Keys;
        }

        public PresentationManager()
        {
            this.dict = new Dictionary<string, PresentationInfo>();
            currentPresentation = null;
        }

        public void PopulateFromJsonList(string jsonList)
        {
            dict.Clear();
            this.currentPresentation = null;
            JArray array = JArray.Parse(jsonList);
            foreach (JObject o in array)
            {
                PresentationInfo info = new PresentationInfo
                {
                    Key = (string)o["id"],
                    Name = (string)o["name"]
                };

                dict[info.Name] = info;
            }
        }

        public bool CanPresent
        {
            get
            {
                return this.currentPresentation != null && !String.IsNullOrEmpty(this.currentPresentation.Key);
            }
        }

    }
}
